package com.skyline.wo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.skyline.common.bean.Page;
import com.skyline.common.util.Constant;
import com.skyline.common.util.ViewPaths;
import com.skyline.common.util.WebHelper;
import com.skyline.user.model.User;
import com.skyline.wo.model.ShortMessage;
import com.skyline.wo.service.ShortMessageService;

@Controller
@RequestMapping("/shortMessage")
public class ShortMessageController {

//	@Value("${view.shortMessage.view}")
//	private String viewShortMessageView;
//
//	@Value("${view.shortMessage.list}")
//	private String listShortMessageView;

	@Autowired
	private ShortMessageService shortMessageService;
	
	private @Value("${shortMessage.list.pageSize}")
	Integer listMessagePageSize;

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public String addShortMessage(String text) {
		User user = (User) WebHelper.getSessionAttribute(null, Constant.SESSION_USER);
		if (user == null || user.getId() == 0) {
			return null;
		} else {
			return shortMessageService.addShortMessage(user.getId(), user.getNickname(), user.getPortrait(), text);
		}
	}
	
	@RequestMapping(value = "/forward", method = RequestMethod.POST)
	@ResponseBody
	public String forwardShortMessage(String comment,String id) {
		User user = (User) WebHelper.getSessionAttribute(null, Constant.SESSION_USER);
		if (user == null || user.getId() == 0) {
			return null;
		} else {
			String forwardId= shortMessageService.forwardShortMessage(id, user.getId(), user.getNickname(), user.getPortrait(),comment);
			return forwardId;
		}
	}

	@RequestMapping(value = "/id/{id}/ajax", method = RequestMethod.GET)
	@ResponseBody
	public ShortMessage ajaxGetShortMessageById(@PathVariable String id) {
		return shortMessageService.getShortMessageById(id);
	}

	@RequestMapping(value = "/view/{id}", method = RequestMethod.GET)
	public ModelAndView getShortMessageById(@PathVariable String id) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName(ViewPaths.SHORTMESSAGE_VIEW);
		ShortMessage message = shortMessageService.getShortMessageById(id);
		mav.addObject("shortMessage", message);
		return mav;
	}

	@RequestMapping(value = "/owner/{ownerId}/ajax", method = RequestMethod.GET)
	@ResponseBody
	public List<ShortMessage> ajaxGetShortMessageByOwnerId(@PathVariable long ownerId,Page page) {
		return shortMessageService.getShortMessageByOwnerId(ownerId,page);
	}

	@RequestMapping(value = "/owner/{ownerId}", method = RequestMethod.GET)
	public ModelAndView getShortMessageByOwnerId(@PathVariable long ownerId,Page page) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName(ViewPaths.SHORTMESSAGE_LIST);
		page.setSize(listMessagePageSize);
		List<ShortMessage> messages = shortMessageService.getShortMessageByOwnerId(ownerId,page);
		mav.addObject("shortMessages", messages);
		mav.addObject("page", page);
		mav.addObject("ownerId",ownerId);
		return mav;
	}

	//FIXME 不要相对路径
	@RequestMapping(value = "/delete/{id}/{curpage}", method = RequestMethod.GET)
	public RedirectView deleteShortMessage(@PathVariable String id,@PathVariable int curpage) {
		RedirectView rv = new RedirectView();
		User user = (User) WebHelper.getSessionAttribute(null, Constant.SESSION_USER);
		if (user != null && user.getId() != 0) {
			shortMessageService.deleteShortMessage(id, user.getId());
			rv.setUrl("../../owner/" + user.getId() + ".html?curpage="+curpage);
		}
		return rv;
	}
}
